/*
 * PlainView.java
 *
 * Created on 23 October 2007, 22:38
 *
 */

package net.ubinow.mflex.widgets;



import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import net.ubinow.mflex.baseui.ICommandBar;
import net.ubinow.mflex.baseui.IWidget;
import net.ubinow.mflex.baseui.WFont;
import net.ubinow.mflex.baseui.WidgetContainer;
import net.ubinow.mflex.events.Event;
import net.ubinow.mflex.util.Constants;
import net.ubinow.mflex.util.MiscUtil;

/**
 * standard view that contains a 'Background Image', 'Header' (made up of a background image, icon, and title - 
 * all of which are optional), 'WidgetContainer' (decorates this object), 'commandbar' (implementation of
 *  the ICommandBar interface)
 * @author Josh
 */
public class PlainView extends AbsView {            
    
    // flags whether a widget has been selected or not - intilially the first selectable widget will be selected if this is false
    private boolean _hasSelectedWidget = false;
    
    /** Creates a new instance of PlainView */
    public PlainView( int x, int y, int width, int height, int headerHeight, Image headerBGImage, 
            Image headerIcon, int alignIcon, String headerText, int alignText, WFont defaultFont ) {                                
        
        super( x, y, width, height, defaultFont );
        
        _headerHeight = headerHeight;        
        _viewableHeight -= _headerHeight;
        
        if( headerBGImage != null ){
            setHeaderImage( headerBGImage );            
        }
        
        if( headerIcon != null ){
            setHeaderIcon( headerIcon, Constants.AD_POS_HEADER_ICON_X, Constants.AD_POS_HEADER_ICON_Y, 
                    alignIcon ); 
        }
        
        if( headerText != null ){
            setHeaderText( headerText, Constants.AD_POS_HEADER_TEXT_X, Constants.AD_POS_HEADER_TEXT_Y, alignText );
        }                 
        
        initWidgetContainer(); 
    }    
    
    public void setFont( WFont font) {
        super.setFont( font );
        
        if( _widgets != null )
            _widgets.setFont( font ); 
    }
    
    public void setDefaultFont( WFont font ){
        super.setDefaultFont( font );
        
        if( _widgets != null )
            _widgets.setFont( font ); 
    }
    
     //<editor-fold defaultstate="collapsed" desc="widget container fields and methods">
    
    private WidgetContainer _widgets = null; 
    
    private void initWidgetContainer(){
        _widgets = new WidgetContainer( 0, _headerHeight, _viewableWidth, 
                _viewableHeight ); 
        
        // TODO: remove the value 10 with the size of the docked commadn bar
        
        _widgets.addEventSubscriber( Event.EVT_ANY, this );
        _widgets.setFont( _bodyFont );
        
        
    }
    
    public void addWidget( IWidget widget ){
        if( _widgets == null )
            return; 
        
        _widgets.addWidget( widget, false );
        
        // if the first widget then set to selected
        if( !_hasSelectedWidget && widget.isSelectable() ){
            _widgets.setFocus( widget );
            _hasSelectedWidget = true; 
        }        
    }     
    
    public WidgetContainer getWidgetContainer(){
        return _widgets;
    }
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="command bar fields and methods">
    
    // TODO: implement the commadn bar 
    
    /**
     * @param cmdBar: object of type ICommandBar
     * @param dock: where to dock 
     **/
    public void setCommandBar( ICommandBar cmdBar, int dock ){
        super.setCommandBar( cmdBar, dock );
        
        // resize the widget container to take into account the command bar 
        if( _widgets != null )
            _widgets.setHeight( _viewableHeight );
    }
    
    //</editor-fold>    
    
    //<editor-fold defaultstate="collapsed" desc="header fields and method">
    
    private int _headerHeight = 0;
    private Image _headerImg = null;
    private Image _headerIcon = null; 
    private int _headerIconX = 0;
    private int _headerIconY = 0;
    private String _headerText = null;
    private int _headerTextX = 0;
    private int _headerTextY = 0;
    
    public void setHeaderImage( Image img ){
        _headerImg = img; 
    }
    
    public void setHeaderIcon( Image ico, int xPadding, int y, int align ){
        _headerIcon = ico;
        
        // work out x
        if( (align & Constants.ALIGN_LEFT) != 0 ){
            _headerIconX = xPadding; 
        }
        else if( (align & Constants.ALIGN_RIGHT) != 0 ){
            _headerIconX = _width - (_headerIcon.getWidth()-xPadding ); 
        }
        else if( (align & Constants.ALIGN_LEFT) != 0 ){
            _headerIconX = (_width-(_headerIcon.getWidth()+xPadding))>>1; 
        }
        else{
            _headerIconX = 0;
        }
        
        _headerIconY = y; 
        
    }
    
    public void setHeaderText( String title, int xPadding, int y, int align ){
        _headerText = title; 
        
        // work out x and y         
        if( (align & Constants.ALIGN_LEFT) == Constants.ALIGN_LEFT ){
            _headerTextX = xPadding; 
        }
        else if( (align & Constants.ALIGN_RIGHT) == Constants.ALIGN_RIGHT ){
            if( _headerFont != null ){
                _headerTextX = _width - (_headerFont.stringWidth(_headerText)-xPadding ); 
            }
        }
        else if( (align & Constants.ALIGN_CENTRE_H) == Constants.ALIGN_CENTRE_H ){
            if( _headerFont != null ){
                _headerTextX = (_width - (_headerFont.stringWidth(_headerText) ))>>1; 
            }
        }
        else{
            _headerTextX = 0;
        }        
        
        _headerTextY = y; 
    }
    
    //</editor-fold>                
    
    //<editor-fold defaultstate="collapsed" desc="processing methods">        

    public boolean handleKeyPressed(int action, int keyCode) {
        boolean handled = _widgets.handleKeyPressed( action, keyCode );                
        return handled; 
    }

    public boolean handleKeyReleased(int action, int keyCode) {
        boolean handled = _widgets.handleKeyReleased( action, keyCode );                
        return handled; 
    }
    
    public void cycle() {
        if( _widgets != null )
            _widgets.cycle();
    }
    
    public void paint(Graphics g, int offsetX, int offsetY, int layer ) {
        
        MiscUtil.getInstance().clip( g, _x + offsetX, _y + offsetY, _width, _height ); 
        
        if( _bgColour != -1 ){
            g.setColor( _bgColour );
            g.drawRect( _x + offsetX, _y + offsetY, _width, _height );
        }
        
        if( _bgImage != null ){
            g.drawImage( _bgImage, _x + offsetX, _y + offsetY, Constants.GFX_ALIGN_TOP_LEFT );
        }                
        
        // TODO: draw widget container 
        //_widgets.paint( g, offsetX, offsetY+_headerHeight, 1 );
        _widgets.paint( g, offsetX, offsetY, 1 );
        
        // draw header 
        if( _headerImg != null ){
            g.drawImage( _headerImg, 0, 0, Constants.GFX_ALIGN_TOP_LEFT );
        }
        
        if( _headerIcon != null ){
            g.drawImage( _headerIcon, _headerIconX, _headerIconY, Constants.GFX_ALIGN_TOP_LEFT );
        }
        
        if( _headerText != null ){
            _headerFont.drawString( g, _headerText, _headerTextX, _headerTextY );
        }
        
        // TODO: draw command bar
        if( _cmdBar != null ){
            _cmdBar.paint( g, offsetX, offsetY, 0 );
        }
        
        MiscUtil.getInstance().releaseClip( g ); 
    }
    
    //</editor-fold>    
    
    //<editor-fold desc="helper methods for adding widgets to the widget container" defaultstate="collapsed">
    
    // TODO:

    //</editor-fold>
    
}
